import {
  getAllDicList,
  postOrganizationInfo,
  postOrganizationTree,
} from '@/api/public';
export default {
  state: {
    dict: {
      state: {},
      plan_node: {},
      ltcsmsizebu: {},
    },
    dictList: {},
    organization: [],
  },
  getters: {
    getDictList: state => code => {
      return state.dictList[code] || {};
    },

    getDict: state => code => {
      return state.dict[code] || {};
    },

    getOrg: state => ({ code, isDict = true, type }) => {
      /**
       * 关键字                类型
       operationgroup     运营部门
       salesgroup         销售组
       kapmgroup          KAPM组
       ltcorg             销售组织
       salesmanage        销售管理
       */
      const options = {
        ltcorg: 'ltcorg',
        sales: 'salesgroup',
        kapm: 'kapmgroup',
        operation: 'operationgroup',
        salesmanage: 'salesmanage',
      };
      type = options[type || 'ltcorg'] || type;
      if (!code) return [];
      let children = [];
      let queues = [];

      function findChildren(tree, code) {
        if (!code || !tree) return;
        if (tree.code === code) {
          queues = queues.concat(tree.children || []);
        } else if (tree.children?.length > 0) {
          tree.children.forEach(element => {
            findChildren(element, code);
          });
        }
      }
      state.organization.forEach(item => findChildren(item, code));
      // findChildren(state.organization[0], code);

      let item = queues.shift();
      while (item) {
        if (item.code?.includes(type)) {
          children.push(item);
        }
        if (item.children && item.children?.length) {
          queues = queues.concat(item.children);
        }
        item = queues.shift();
      }
      // function findChildren(tree, code) {
      //   if (tree.code === code) {
      //     children = tree.children;
      //   } else if (tree.children?.length > 0) {
      //     tree.children.forEach(element => {
      //       findChildren(element, code);
      //     });
      //   }
      // }
      // if (type === 'sales') {
      //   code = `${code}sales`;
      //   findChildren(state.organization[0] || {}, code);
      //   children = children.filter(item => !item.code.includes('manage'));
      // } else if (type === 'kapm') {
      //   code = `${code}pmd`;
      //   findChildren(state.organization[0] || {}, code);
      //   children = children.filter(item => !item.code.includes('layout'));
      // } else if (type === 'operation') {
      //   code = `${code}operation`;
      //   findChildren(state.organization[0] || {}, code);
      // } else {
      //   findChildren(state.organization[0] || {}, code);
      // }
      if (isDict) {
        return children.reduce((pre, cur) => {
          pre[cur.code] = cur.name;
          return pre;
        }, {});
      } else {
        const copy = JSON.parse(JSON.stringify(children));
        return copy.sort((a, b) => (a.code > b.code ? 1 : -1)) || [];
      }
    },
  },
  mutations: {
    setDicData(state, value) {
      state.dict = value;
    },

    setDicList(state, value) {
      state.dictList = value;
    },

    setOrganization(state, value) {
      state.organization = value;
    },
  },
  actions: {
    async getAllDicList({ commit }) {
      const result = await getAllDicList();
      const data = result?.data?.data || {};
      const map = Object.keys(data).reduce((p, c) => {
        const obj = data[c].reduce((pre, cur) => {
          pre[cur.value] = cur.label;
          return pre;
        }, {});
        p[c] = obj;
        return p;
      }, {});
      commit('setDicData', map);
      commit('setDicList', data);
    },

    async getOrganizationTree({ commit }, code) {
      const result = await postOrganizationTree({
        code,
      });
      const data = result?.data?.data || [];
      commit('setOrganization', data);
    },
  },
};
